<?php

namespace App\Services\Core\Store\Eastrongzi;

use Prophecy\Exception\Exception;
use App\Helpers\Http\HttpCurl;

/**
*
* 东方融资服务
*
*/
class EastrongziService
{

    // 三方提供来源标识
    protected $UtmSource = 255;
    // 东方融资密钥
    private static  $east_secret_key = 'rongzi.com_8763';
    // 东方金融检查是否注册地址
    protected $EastisregisteUrl = 'http://mirzr.rongzi.com/rzr/Transfer/IsRegistered';
    // 东方金融注册地址
    protected $EastregisteUrl = 'http://mirzr.rongzi.com/rzr/Transfer/Register';


   /**
    * 东方融资对接推送——检查是否注册
    *
    */ 
    public function isregistered($phone)
    {

        // 东方融资请求参数
        $CellPhoneNumber = $phone;
        $TimeStamp       = time();
        $params = array(
            'CellPhoneNumber' => $CellPhoneNumber,
            'TimeStamp'       => $TimeStamp,
            'Signature'       => $this->setisregSignature($CellPhoneNumber,$TimeStamp)
        );
        $params = json_encode($params);
        // 发送请求
        $response = HttpCurl::i()->POST($this->EastisregisteUrl ,$params ,$contentType = 'application/json;charset=UTF-8');

        return ($response);
    }


    /**
    * 东方融资对接推送——检查是否注册数字签名处理
    *
    */ 
    public function setisregSignature($CellPhoneNumber,$TimeStamp)
    {
        return md5($CellPhoneNumber.$TimeStamp.self::$east_secret_key);
    }

    /**
    * 东方融资对接推送——注册
    *
    */ 
    public function register($data)
    {
        // 东方融资请求参数
        $TimeStamp = date('yyyyMMddHHmmss');
        $params    = $data;
        // 声明请求参数(来源标识、时间戳、数字签名)
        $params['UtmSource'] = $this->UtmSource;
        $params['TimeStamp'] = $TimeStamp;
        $params['Signature'] = $this->setregSignature($params['CityName'],$params['CellPhoneNumber'],$params['RealName'],$params['Gender'],$params['LoanAmount'],$params['TimeStamp']);

        $params = json_encode($params,JSON_UNESCAPED_UNICODE );
        // 发送请求
        $response = HttpCurl::i()->POST($this->EastregisteUrl ,$params ,$contentType = 'application/json;charset=UTF-8');

        return ($response);
    }

    /**
    * 东方融资对接推送——注册数字签名处理
    *
    */ 
    public function setregSignature($CityName,$CellPhoneNumber,$RealName,$Gender,$LoanAmount,$TimeStamp)
    {
        return md5($CityName.$CellPhoneNumber.$RealName.$Gender.$LoanAmount.$this->UtmSource.$TimeStamp.self::$east_secret_key);
    }    


}
